# -*- coding:utf-8 -*-
import logging

from mq import BaseConsumer
from setting import admin_host
from util.date_util import curr_now
from util.mail_sender import MailSender

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)s lineno:%(lineno)d %(message)s")
logger = logging.getLogger('mq.consumer.hotel.mail')

class HotelMailNotice(BaseConsumer):

    def init(self):
        self.audit_url = u"{}/#/hotelManage/hotelRecord/recordList".format(admin_host)
        self.mail = MailSender()
        self.receivers = ["982010@qq.com"]

    def _get_hotel_exchange_record(self, ids=[]):
        return self.db.query("select hcr.name, hcr.mobile, hcr.check_in_date, c.code, h.name as hotel_name, h.city, h.district, h.address "
                             "  from t_hotel_code_record hcr "
                             "  left join t_code c on c.id = hcr.code_id "
                             "  left join t_hotel h on h.id = hcr.hotel_id "
                             " where hcr.code_id in %(ids)s ", ids=ids)

    def _get_admin_emails(self, ids=[]):
        staffs = self.db.query("select email from t_staff "
                               " where company_id in (select manufacturer_company_id "
                               "                        from t_hotel_code "
                               "                       where id in %(ids)s) and role_type = 2", ids=ids)

        logger.debug(staffs)
        return map(lambda s: s.email, staffs)

    def _prepare_mail_txt(self, data):
        """
        酒店兑换邮件通知

        :param data: {"ids": "1,2,3"}
        :return:
        """
        if not data.ids:
            logger.debug("ids is empty")
            return True

        ids = str(data.ids).split(",")
        hotel_exchange_records = self._get_hotel_exchange_record(ids)
        logger.debug(hotel_exchange_records)

        self.receivers = self._get_admin_emails(ids)

        hotel_name = u''
        html = u''

        html += u'<p><table id="tab" style="width:800px;" cellpadding="2" cellspacing="0" align="left" border="1" bordercolor="#000000">'
        html += u'<tbody><tr>'
        html += u'<td>酒店信息</td>'
        html += u'<td style="text-align:center;font-size:14px;">姓名</td>'
        html += u'<td style="text-align:center;font-size:14px;">手机号</td>'
        html += u'<td style="text-align:center;font-size:14px;">激活码</td>'
        html += u'<td style="text-align:center;font-size:14px;">入住时间</td>'
        html += u'</tr>'

        for record in hotel_exchange_records:
            html += u'<tr>'
            html += u'<td style="text-align:left;font-size:14px;">{} {} {} {}</td>'.format(record.city, record.district, record.address, record.hotel_name)
            html += u'<td style="text-align:left;font-size:14px;">{}</td>'.format(record.name)
            html += u'<td style="text-align:left;font-size:14px;">{}</td>'.format(record.mobile)
            html += u'<td style="text-align:left;font-size:14px;">{}</td>'.format(record.code)
            html += u'<td style="text-align:left;font-size:14px;">{}</td>'.format(record.check_in_date)
            html += u'</tr>'

            hotel_name = record.hotel_name

        html += u'</tbody></table></p>'
        html += u'<a href="{}">点击审核订单>></a>'.format(self.audit_url)
        return html, hotel_name

    def runner(self, data):

        html, hotel_name = self._prepare_mail_txt(data)
        if self.receivers:
            subject = u'「{}」酒店兑换通知-{}'.format(hotel_name, curr_now())
            self.mail._send_mail(self.receivers, subject, html)

        return True